#!/bin/sh

ceph_bin="/usr/bin/ceph"
rados_bin="/usr/bin/rados"
zstor_info_dir="/dev/shm/zstorage"


if [ ! -d ${zstor_info_dir} ];then
  mkdir -p ${zstor_info_dir}
fi

# Initialising variables
# See: http://ceph.com/docs/master/rados/operations/pg-states/
creating=0
active_clean=0
inactive=0
unclean=0
down=0
replay=0
splitting=0
scrubbing=0
degraded=0
inconsistent=0
peering=0
repair=0
recovering=0
backfill=0
waitBackfill=0
incomplete=0
stale=0
remapped=0

# Get data
pginfo=$(echo -n "  pgmap $($ceph_bin pg stat)" | sed -n "s/.*pgmap/pgmap/p")
pgtotal=$(echo $pginfo | cut -d':' -f2 | sed 's/[^0-9]//g')
pgstats=$(echo $pginfo | cut -d':' -f3 | cut -d';' -f1| sed 's/ /\\ /g')
pggdegraded=$(echo $pginfo | sed -n '/degraded/s/.* degraded (\([^%]*\)%.*/\1/p')
if [[ "$pggdegraded" == "" ]]
then
  pggdegraded=0
fi
# unfound (0.004%)
pgunfound=$(echo $pginfo | cut -d';' -f2|sed -n '/unfound/s/.*unfound (\([^%]*\)%.*/\1/p')
if [[ "$pgunfound" == "" ]]
then
  pgunfound=0
fi

# write kbps B/s
rdbps=$(echo $pginfo | sed -n '/pgmap/s/.* \([0-9]* .\?\)B\/s rd.*/\1/p' | sed -e "s/K/*1000/ig;s/M/*1000*1000/i;s/G/*1000*1000*1000/i;s/E/*1000*1000*1000*1000/i" | bc)
if [[ "$rdbps" == "" ]]
then
  rdbps=0
fi

# write kbps B/s
wrbps=$(echo $pginfo | sed -n '/pgmap/s/.* \([0-9]* .\?\)B\/s wr.*/\1/p' | sed -e "s/K/*1000/ig;s/M/*1000*1000/i;s/G/*1000*1000*1000/i;s/E/*1000*1000*1000*1000/i" | bc)
if [[ "$wrbps" == "" ]]
then
  wrbps=0
fi

# ops
ops=$(echo $pginfo | sed -n '/pgmap/s/.* \([0-9]*\) op\/s.*/\1/p')
if [[ "$ops" == "" ]]
then
  ops=0
fi

# Explode array
IFS=', ' read -a array <<< "$pgstats"
for element in "${array[@]}"
do
    element=$(echo "$element" | sed 's/^ *//g')
    # Get elements
    number=$(echo $element | cut -d' ' -f1)
    data=$(echo $element | cut -d' ' -f2)

    # Agregate data
    if [ "$(echo $data | grep creating | wc -l)" == 1 ]
    then
	  creating=$(echo $creating+$number|bc)
    fi

    if [ "$(echo $data | grep active+clean | wc -l)" == 1 ]
    then
          active_clean=$(echo $active_clean+$number|bc)
    fi

    if [ "$(echo $data | grep inactive | wc -l)" == 1 ]
    then
	  inactive=$(echo $inactive+$number|bc)
    fi

    if [ "$(echo $data | grep unclean | wc -l)" == 1 ]
    then
	 unclean=$(echo $unclean+$number|bc)
    fi

    if [ "$(echo $data | grep down | wc -l)" == 1 ]
    then
	  down=$(echo $down+$number|bc)
    fi

    if [ "$(echo $data | grep replay | wc -l)" == 1 ]
    then
	  replay=$(echo $replay+$number|bc)
    fi

    if [ "$(echo $data | grep splitting | wc -l)" == 1 ]
    then
	  splitting=$(echo $splitting+$number|bc)
    fi

    if [ "$(echo $data | grep scrubbing | wc -l)" == 1 ]
    then
	  scrubbing=$(echo $scrubbing+$number|bc)
    fi

    if [ "$(echo $data | grep degraded | wc -l)" == 1 ]
    then
	  degraded=$(echo $degraded+$number|bc)
    fi

    if [ "$(echo $data | grep inconsistent | wc -l)" == 1 ]
    then
	  inconsistent=$(echo $inconsistent+$number|bc)
    fi

    if [ "$(echo $data | grep peering | wc -l)" == 1 ]
    then
	  peering=$(echo $peering+$number|bc)
    fi

    if [ "$(echo $data | grep repair | wc -l)" == 1 ]
    then
	  repair=$(echo $repair+$number|bc)
    fi

    if [ "$(echo $data | grep recovering | wc -l)" == 1 ]
    then
	  recovering=$(echo $recovering+$number|bc)
    fi

    if [ "$(echo $data | grep backfill | wc -l)" == 1 ]
    then
	  backfill=$(echo $backfill+$number|bc)
    fi

    if [ "$(echo $data | grep "wait-backfill" | wc -l)" == 1 ]
    then
	  waitBackfill=$(echo $waitBackfill+$number|bc)
    fi

    if [ "$(echo $data | grep incomplete | wc -l)" == 1 ]
    then
	  incomplete=$(echo $incomplete+$number|bc)
    fi

    if [ "$(echo $data | grep stale | wc -l)" == 1 ]
    then
	  stale=$(echo $stale+$number|bc)
    fi

    if [ "$(echo $data | grep remapped | wc -l)" == 1 ]
    then
	  remapped=$(echo $remapped+$number|bc)
    fi
done

status=$($ceph_bin health | awk '{print $1}')
case $status in
    HEALTH_OK)
        flock -x -o -w 1 ${zstor_info_dir}/health.lock -c "echo 1 >${zstor_info_dir}/health.dat"       
        ;;
    HEALTH_WARN)
        flock -x -o -w 1 ${zstor_info_dir}/health.lock -c "echo 2 >${zstor_info_dir}/health.dat"
        ;;
    HEALTH_ERR)
        flock -x -o -w 1 ${zstor_info_dir}/health.lock -c "echo 3 >${zstor_info_dir}/health.dat"
        ;;
    *)
        flock -x -o -w 1 ${zstor_info_dir}/health.lock -c "echo -1 >${zstor_info_dir}/health.dat"
        ;;
esac

cluster_total=$($rados_bin df | grep "total space"| awk '{print $3}')
if [[ "$cluster_total" != "" ]]
then
    flock -x -o -w 1 ${zstor_info_dir}/cluster_total.lock -c "echo $cluster_total >${zstor_info_dir}/cluster_total.dat"
fi

cluster_used=$($rados_bin df | grep "total used"| awk '{print $3}')
if [[ "$cluster_used" != "" ]]
then
    flock -x -o -w 1 ${zstor_info_dir}/cluster_used.lock -c "echo $cluster_used >${zstor_info_dir}/cluster_used.dat"
fi

cluster_free=$($rados_bin df | grep "total avail"| awk '{print $3}')
if [[ "$cluster_free" != "" ]]
then
    flock -x -o -w 1 ${zstor_info_dir}/cluster_free.lock -c "echo $cluster_free >${zstor_info_dir}/cluster_free.dat"
fi

cluster_used_percent=$($ceph_bin osd df|grep " TOTAL"|awk '{print $5}')
if [[ "$cluster_used_percent" != "" ]]
then
    flock -x -o -w 1 ${zstor_info_dir}/cluster_used_percent.lock -c "echo $cluster_used_percent >${zstor_info_dir}/cluster_used_percent.dat"
fi

MON_ACTIVE=$($ceph_bin -s | grep -A1 monmap  | grep quorum | awk '{print $5}'|tr , " "|wc -w)
if [[ "$MON_ACTIVE" != "" ]]
then
    flock -x -o -w 1 ${zstor_info_dir}/mon.lock -c "echo $MON_ACTIVE >${zstor_info_dir}/mon.dat"
else
    flock -x -o -w 1 ${zstor_info_dir}/mon.lock -c "echo 0 >${zstor_info_dir}/mon.dat"
fi

MON_COUNT=$($ceph_bin -s | grep monmap|awk '{print $6}'|tr , " "|wc -w)
if [[ "$MON_COUNT" != "" ]]
then
    flock -x -o -w 1 ${zstor_info_dir}/mon_count.lock -c "echo $MON_COUNT >${zstor_info_dir}/mon_count.dat"
else
    flock -x -o -w 1 ${zstor_info_dir}/mon_count.lock -c "echo 0 >${zstor_info_dir}/mon_count.dat"
fi

MON_PROC_COUNT=$(ps -ef |grep ceph-mon | grep -v grep | grep -v bash | wc -l)
if [[ "$MON_PROC_COUNT" != "" ]]
then
    flock -x -o -w 1 ${zstor_info_dir}/mon_proc_count.lock -c "echo $MON_PROC_COUNT >${zstor_info_dir}/mon_proc_count.dat"
else
    flock -x -o -w 1 ${zstor_info_dir}/mon_proc_count.lock -c "echo 0 >${zstor_info_dir}/mon_proc_count.dat"
fi

ceph_osd_count=$($ceph_bin osd dump |grep "^osd"| wc -l)
flock -x -o -w 1 ${zstor_info_dir}/osd_count.lock -c "echo $ceph_osd_count >${zstor_info_dir}/osd_count.dat"

OSD_PROC_COUNT=$(ps -ef |grep ceph-osd | grep -v grep | grep -v bash | wc -l)
if [[ "$OSD_PROC_COUNT" != "" ]]
then
    flock -x -o -w 1 ${zstor_info_dir}/osd_proc_count.lock -c "echo $OSD_PROC_COUNT >${zstor_info_dir}/osd_proc_count.dat"
else
    flock -x -o -w 1 ${zstor_info_dir}/osd_proc_count.lock -c "echo 0 >${zstor_info_dir}/osd_proc_count.dat"
fi

OSD_UP=$($ceph_bin osd dump |grep "^osd"| awk '{print $1 " " $2 " " $3}'|grep up|wc -l)
#UP_PER=$(echo "scale=2; $OSD_UP*100/$ceph_osd_count" |bc)
if [[ "$OSD_UP" != "" ]]
then
    flock -x -o -w 1 ${zstor_info_dir}/osd_up.lock -c "echo $OSD_UP >${zstor_info_dir}/osd_up.dat"
else
    flock -x -o -w 1 ${zstor_info_dir}/osd_up.lock -c "echo "0" >${zstor_info_dir}/osd_up.dat"
fi

OSD_IN=$($ceph_bin osd dump |grep "^osd"| awk '{print $1 " " $2 " " $3}'|grep in|wc -l)
#IN_PER=$(echo "scale=2; $OSD_IN*100/$ceph_osd_count" | bc)
if [[ "$OSD_IN" != "" ]]
then
    flock -x -o -w 1 ${zstor_info_dir}/osd_in.lock -c "echo $OSD_IN >${zstor_info_dir}/osd_in.dat"
else
    flock -x -o -w 1 ${zstor_info_dir}/osd_in.lock -c "echo "0" >${zstor_info_dir}/osd_in.dat"
fi

flock -x -o -w 1 ${zstor_info_dir}/degraded_percent.lock -c "echo $pggdegraded >${zstor_info_dir}/degraded_percent.dat"
flock -x -o -w 1 ${zstor_info_dir}/pgtotal.lock -c "echo $pgtotal >${zstor_info_dir}/pgtotal.dat"
flock -x -o -w 1 ${zstor_info_dir}/creating.lock -c "echo $creating >${zstor_info_dir}/creating.dat"
flock -x -o -w 1 ${zstor_info_dir}/active_clean.lock -c "echo $active_clean >${zstor_info_dir}/active_clean.dat"
flock -x -o -w 1 ${zstor_info_dir}/inactive.lock -c "echo $inactive >${zstor_info_dir}/inactive.dat"
flock -x -o -w 1 ${zstor_info_dir}/unclean.lock -c "echo $unclean >${zstor_info_dir}/unclean.dat"
flock -x -o -w 1 ${zstor_info_dir}/down.lock -c "echo $down >${zstor_info_dir}/down.dat"
flock -x -o -w 1 ${zstor_info_dir}/replay.lock -c "echo $replay >${zstor_info_dir}/replay.dat"
flock -x -o -w 1 ${zstor_info_dir}/splitting.lock -c "echo $splitting  >${zstor_info_dir}/splitting.dat"
flock -x -o -w 1 ${zstor_info_dir}/scrubbing.lock -c "echo $scrubbing >${zstor_info_dir}/scrubbing.dat"
flock -x -o -w 1 ${zstor_info_dir}/degraded.lock -c "echo $degraded >${zstor_info_dir}/degraded.dat"
flock -x -o -w 1 ${zstor_info_dir}/inconsistent.lock -c "echo $inconsistent >${zstor_info_dir}/inconsistent.dat"
flock -x -o -w 1 ${zstor_info_dir}/peering.lock -c "echo $peering >${zstor_info_dir}/peering.dat"
flock -x -o -w 1 ${zstor_info_dir}/repair.lock -c "echo $repair >${zstor_info_dir}/repair.dat"
flock -x -o -w 1 ${zstor_info_dir}/recovering.lock -c "echo $recovering >${zstor_info_dir}/recovering.dat"
flock -x -o -w 1 ${zstor_info_dir}/backfill.lock -c "echo $backfill >${zstor_info_dir}/backfill.dat"
flock -x -o -w 1 ${zstor_info_dir}/waitBackfill.lock -c "echo $waitBackfill >${zstor_info_dir}/waitBackfill.dat"
flock -x -o -w 1 ${zstor_info_dir}/incomplete.lock -c "echo $incomplete >${zstor_info_dir}/incomplete.dat"
flock -x -o -w 1 ${zstor_info_dir}/stale.lock -c "echo $stale >${zstor_info_dir}/stale.dat"
flock -x -o -w 1 ${zstor_info_dir}/remapped.lock -c "echo $remapped >${zstor_info_dir}/remapped.dat"
flock -x -o -w 1 ${zstor_info_dir}/ops.lock -c "echo $ops >${zstor_info_dir}/ops.dat"
flock -x -o -w 1 ${zstor_info_dir}/wrbps.lock -c "echo $wrbps >${zstor_info_dir}/wrbps.dat"
flock -x -o -w 1 ${zstor_info_dir}/rdbps.lock -c "echo $rdbps >${zstor_info_dir}/rdbps.dat"


